<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Address;
use App\Model\User_address;

/**
 * Class AddressController
 * @package App\Http\Controllers\User
 * @User wuqin
 * @Date 2018/4/13 0013
 * @Time 11:36
 */
class AddressController extends Controller
{
    /**
     * 方法 : list
     * @User wuqin
     * @DateTime 2018-04-13 11:36
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function list(Request $request)
    {
        $data = array();
        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);
        $user_addModel = new User_address();
        $where = array(
            array("where", "user_id", "=", $user["user_id"])
        );

        $user_address = $user_addModel->get($where);
        foreach ($user_address as $key => &$value) {
            $value["phone"] = substr_replace($value["phone"], "****", 3, 4);
        }
        unset($value);
        $data["address"] = $user_address;
        return view("Address.list", $data);
    }

    /**
     * 方法 : add
     * @User wuqin
     * @DateTime 2018-04-13 11:36
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add(Request $request)
    {
        $user = session('user_info');
        if (empty($user))
            return $this->error_url('/', '请重新登录', 3);

        $data = array();
        $addressModel = new Address();
        $addressRes = $addressModel->get_provinces();
        foreach ($addressRes as $key => $value) {
            if($value['parent'] == '-1'){
                $address['p_p'][$key] = $value;
                continue;
            }
            $address['p_'.$value['parent']][$key] = $value;
        }
        $addressRes = $addressModel->get_citys();
        foreach ($addressRes as $key => $value) {
            if($value['parent'] == '-1'){
                $address['p_p'][$key] = $value;
                continue;
            }
            $address['p_'.$value['parent']][$key] = $value;
        }
        $data['address'] = $address;
        return view("Address.add",$data);
    }
}
